package com.module.msg.view

import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.ImageView
import com.module.app.base.BaseActivity
import com.module.app.ext.notNull
import com.module.app.utils.JumpUtils
import com.module.base.utils.CollectionUtil
import com.module.app.utils.GlideUtils
import com.module.base.utils.imagepicker.CommonImagePickerUtils
import com.module.frame.ext.observe
import com.module.frame.ext.postEvent
import com.module.msg.R
import com.module.msg.bean.IMsgType
import com.module.msg.bean.MsgBean
import com.module.msg.databinding.MsgActivityAddMsgBinding
import com.module.msg.ext.myMsgFlow
import com.module.msg.model.AddMsgViewModel
import com.module.msg.utils.EXTRA_BEAN
import com.module.third.bean.IThirdType

class AddMsgActivity : BaseActivity<AddMsgViewModel, MsgActivityAddMsgBinding>() {

    //有值代表编辑
    private val msgBean: MsgBean? by lazy {
        intent.getSerializableExtra(EXTRA_BEAN) as MsgBean?
    }
    private var url: String? = null
    private var isVideo = false

    override fun bingViewModel() {
        mDatabind.vm = mViewModel
    }

    override fun initView() {
        super.initView()
        msgBean.notNull({
            when {
                msgBean?.status === IMsgType.IStatus.notice -> {
                    setMyTitle(R.string.msg_status_notice)
                }
                msgBean?.status === IMsgType.IStatus.activity -> {
                    setMyTitle(R.string.msg_status_activity)
                }
                else -> {
                    setMyTitle(R.string.msg_title)
                }
            }
            mDatabind.tvJump.apply {
                visibility = if (msgBean?.jump === IThirdType.IJump.NO) View.GONE else View.VISIBLE
                text = msgBean?.btnName
            }
            mDatabind.etTitle.apply {
                setText(msgBean?.title)
                isEnabled = false
            }
            mDatabind.etContent.apply {
                setText(msgBean?.content)
                isEnabled = false
            }
            mDatabind.tvMsg.apply {
                visibility = if (msgBean?.message.isNullOrEmpty()) View.GONE else View.VISIBLE
                if (!msgBean?.message.isNullOrEmpty()) {
                    text = msgBean?.message?.replace("\\n", "\n")
                }
            }
            msgBean?.let {
                setImage(it.url, it.isVideo)
            }
            if (TextUtils.isEmpty(msgBean?.url)) {
                mDatabind.layoutAddImg.visibility = View.GONE
            }
        }, {
            setMyTitle(R.string.msg_publish_title)
            setRightText(R.string.msg_publish, View.OnClickListener { //执行发布
                mViewModel.publish(
                    mDatabind.etTitle.text.toString(),
                    mDatabind.etContent.text.toString(),
                    url,
                    isVideo
                )
            })
        })

        if (msgBean != null && !msgBean?.url.isNullOrEmpty() && msgBean?.isVideo == true) {
            mDatabind.ivPlay.visibility = View.VISIBLE
        } else {
            mDatabind.ivPlay.visibility = View.GONE
        }
    }

    override fun initListener() {
        super.initListener()
        observe(mViewModel.publishFlow) {
            publishSuc()
        }
        mDatabind.etTitle.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
            override fun onTextChanged(s: CharSequence, i: Int, i1: Int, i2: Int) {
                try {
                    //设置数据
                    mDatabind.tvCount.text = s.length.toString() + "/30"
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

            override fun afterTextChanged(editable: Editable) {}
        })
    }


    fun onClickImg(view: View) {
        //暂时不处理
    }

    /**
     * 设置图片
     */
    private fun setImage(url: String?, isVideo: Boolean) {
        this.url = url
        this.isVideo = isVideo
        if (url.isNullOrEmpty()) {
            mDatabind.ivImg.scaleType = ImageView.ScaleType.CENTER
            mDatabind.ivImg.setImageResource(R.drawable.msg_photo)
        } else {
            mDatabind.ivImg.scaleType = ImageView.ScaleType.CENTER_CROP
            com.module.app.utils.GlideUtils.loadNet(getContext(), mDatabind.ivImg, url)
        }
    }

    private fun publishSuc() {
        postEvent(myMsgFlow, true)
        finish()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (CommonImagePickerUtils.onActivityResult(requestCode, resultCode)) {
            // 相册结果回调
            val list = CommonImagePickerUtils.getList(data)
            //图片选择回调，主线程
            if (CollectionUtil.isEmptyOrNull(list)) {
                return
            }
            setImage(list[0]!!.realPath, list[0]!!.duration > 0)
        }
    }

    fun onClickJump(view: View) {
        if (msgBean == null) {
            return
        }
        msgBean?.let {
            com.module.app.utils.JumpUtils.jump(getContext(), it.jump, msgBean?.getObject())
        }
    }

}